﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using Proxy;
using System;

namespace HomagChina.Nesting
{
    public class SettingsDialogViewModel : NotifyPropertyChangedBase
    {
        #region
        //Handle
        InsertInfoHandle Handle_Insert;
        SearchInfoHandle Handle_Search;
        ModifyInfoHandle Handle_Modify;
        CommonHandle commonHandle;
        #endregion

        //属性
        #region
        private int _ID;
        private string _IP;
        private int _Port;
        private string _Topic;
        private string _Describe;
        private Boolean _State;
        private Boolean _AgainstState;
        //是否为编辑状态
        private bool _IsEdit;
        public int Port { get => _Port; set { _Port = value; OnPropertyChanged(); } }
        public string Topic { get => _Topic; set { _Topic = value; OnPropertyChanged(); } }
        public string Describe { get => _Describe; set { _Describe = value; OnPropertyChanged(); } }
        public int ID { get => _ID; set { _ID = value; OnPropertyChanged(); } }
        public Boolean State { get => _State; set { _State = value; OnPropertyChanged(); } }
        public string IP { get => _IP; set { _IP = value; OnPropertyChanged(); } }
        public bool AgainstState { get => _AgainstState; set { _AgainstState = value; OnPropertyChanged(); } }
        public bool IsEdit { get => _IsEdit; set { _IsEdit = value; OnPropertyChanged(); } }
        //提交
        public RelayCommand ApplyCommand { get; }

        #endregion

        //构造函数
        public SettingsDialogViewModel(SetttingsModel SelectItem, Boolean IsEdit)
        {
            if (SelectItem != null)
            {
                this.IP = SelectItem.Ip;
                this.ID = SelectItem.Id;
                this.Port = SelectItem.Port;
                this.Topic = SelectItem.Topic;
                this.Describe = SelectItem.Describe;
                this.State = SelectItem.State == 0;
                this.AgainstState = SelectItem.State == 1;
            }
            this.IsEdit = IsEdit;
            //提交
            ApplyCommand = new RelayCommand(Apply);
            //工具类函数
            commonHandle = new CommonHandle();
            //查询
            Handle_Search = new SearchInfoHandle();
            //写入
            Handle_Insert = new InsertInfoHandle();
            //更新
            Handle_Modify = new ModifyInfoHandle();
        }

        //提交函数
        private void Apply()
        {
            var t = new T_SettingsConfig
            {
                Id = this.ID,
                Ip = this.IP,
                Port = this.Port,
                Topic = this.Topic,
                Describe = this.Describe,
                State = this.State ? 0 : 1,
                CreationDate = DateTime.Now,
                ModificationDate = DateTime.Now
            };

            if (IsEdit)
            {
                if (Handle_Modify.ModifySettingsInfo(t))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show("Congratulations, the information has been successfully modified！");
                    new SettingsViewModel().Refresh();
                    commonHandle.CloseDiaLog("SettingsDialog");
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show("Sorry, information modification failed, please try again or contact the administrator!");
                }
            }
            else
            {
                if (Handle_Insert.InsertSettingInfos(t))
                {
                    commonHandle.CloseDiaLog("SettingsDialog");
                    HomagGroup.Base.UI.Windows.MessageBox.Show("Congratulations, information added successfully！");
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show("Sorry, information addition failed, please try again or contact administrator！");
                }
            }
        }
    }
}
